
//重置验证码图片
function changeImg(nofocus)
{
    var parentSelector = "#_form_login";
    $(parentSelector+ ' [name=login_img]').attr('src', '/seccode/makecode.html?nchash=&t=' + Math.random());
    if( !nofocus )
    {
        $(parentSelector+' input[name=authnum]').val('').focus();
    }
}

function doLogin() {
    var parentSelector = "#_form_login";
    var username_login = $.trim($('#username_login').val());
    var passwd_login = $.trim($('#passwd_login').val());
    var authnum_login = $.trim($(parentSelector+' input[name=authnum]').val());
    if ( username_login == '') {
        _alert('用户名不能为空!');
        return false;
    }
    if ( passwd_login == '') {
        _alert('密码不能为空!');
        return false;
    }
    var param = {
        login:username_login,
        pass:passwd_login,
        authnum:"",
        form_submit:"ok"
    };
    if(  $(parentSelector+' input[name=authnum]').css("display") == "block" )
    {
        if (authnum_login == '') {
            _alert('验证码不能为空!');
            return false;
        }
        if (!/^[0-9]{4}$/.test(authnum_login)) {
            _alert('请输入4位数字验证码!');
            $(parentSelector+' input[name=authnum]').val("");
            return false;
        }
        param.authnum = authnum_login;
    }
    $("#btn_login").attr("disabled", true);
    $.ajax({
        url:"/login/index.html",
        type:'post',
        data:param,
        dataType: 'JSON',
        success: function (data) {
            try
            {
                if ( session_timeout(data) === false )
                {
                    return false;
                }
            } catch(e){ console.log(e);}
            $("#btn_login").attr("disabled", false);
            if (data.hasOwnProperty('Result') && data.Result)
            {
                //登陆成功
                _alert("登录成功",function(){
                    __openWin('home','/');
                });
            }
            else
            {
                //登陆错误
                _alert( data.Desc );
                changeImg();
            }
        },
        error: function (XMLHttpRequest, status) {
            $("#btn_login").attr("disabled", false);
            if(status=='timeout'){
                _alert('系统繁忙,请重新操作!');
                return false;
            }
        }
    });
}


$(function(){
    //下面是绑定事件
    var parentSelector = "#_form_login";
    //获取新的验证码
    $(parentSelector+ ' [name=login_img]').on("click",function(){
        changeImg();
    });
    //按钮 - 提交登录
    $(parentSelector+ " #btn-sub").on("click",function(){
        doLogin();
    });
    //验证码输入框
    $(parentSelector+" input[name=\"authnum\"]").bind("focus",function(){
        if($(this).val()=="")
        {
            changeImg(true); // 注意这里一定要给参数 true ，否则会出现 事件 无限递归 的触发
        }
        try{if(event.preventDefault){event.preventDefault();}else{event.returnValue = false;}}catch(e){}
        $(parentSelector+" [name=yzm_img_div]").show();
        $(parentSelector+" [name=tip_yzm_img]").hide();
    });
    //重置按钮
/*    $("#btn_clean").bind("click",function(){
        $('#username_login').val("");
        $('#passwd_login').val("");
        changeImg();
    });*/

    //回车事件
    $(parentSelector).get(0).onkeydown = function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if( e.keyCode == 9 )
        {
            try{if(e.preventDefault){e.preventDefault();}else{e.returnValue = false;}}catch(e){}
        }
        if(e && (e.keyCode == 13 || e.keyCode == 9 )){ // enter 键
            var username_login = $.trim($('#username_login').val());
            var passwd_login = $.trim($('#passwd_login').val());
            var authnum_login = $.trim($(parentSelector+' input[name=authnum]').val());
            if ( username_login == '') {
                $('#username_login').focus();
                return;
            }
            if ( passwd_login == '') {
                $('#passwd_login').focus();
                return;
            }
            if ( authnum_login == '') {
                $(parentSelector+' input[name=authnum]').focus();
                return;
            }
            if (!/^[0-9]{4}$/.test(authnum_login)) {
                _alert('验证码输入有误!');
                return;
            }
            doLogin();
        }
    };
   // changeImg();
});